package com.seari.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.seari.bean.User;
import com.seari.pojo.MessageBean;
import com.seari.service.UserService;
import com.seari.utils.JwtUtil;
import com.seari.utils.StringHelper;

@RestController
public class MockController
{
    @Autowired
    private UserService userService;
    
	@PostMapping("api/testUser")
	public MessageBean testUser()
	{
		MessageBean messageBean = new MessageBean();
		messageBean.setCode("200");
		messageBean.setMessage("mock test user");
		return messageBean;
	}
	
	@PostMapping("mock/testLogin")
	public MessageBean testLogin(HttpServletRequest req,HttpServletResponse resp)
	{
		MessageBean messageBean = new MessageBean();
		messageBean.setCode("200");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String stationName = req.getParameter("stationName");
        if(StringUtils.isBlank(stationName))
        {
        	//默认为OCC站进行登陆
        	stationName = "occ";
        }
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password))
        {
        	messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
        	messageBean.setMessage("用户名或密码为空");
            return messageBean;
        }
        User user = userService.getUserByLogin(userName, password, stationName);
        if(null != user && user.getUid() > 0 )
        {
        	messageBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        	String jwt = JwtUtil.createJWT(UUID.randomUUID().toString().replace("-", ""), userName, stationName);
        	//user.setToken(jwt);
        	resp.addHeader("access_token", jwt);
        	messageBean.setData(user);
        }else
        {
        	messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
        	messageBean.setMessage("用户密码不匹配或无此用户");
        }
        messageBean.appendMessage("mock testLogin");
		return messageBean;
	}
}
